// Server.h

#ifndef SERVER_H_INCLUDED
#define SERVER_H_INCLUDED

#include "stdafx.h"
#include "Socket.h"
#include "Utility.h"
#include <vector>
//#include "../zlib/gzip.h"

//using namespace zlib;


struct Request
{
	//Header
	std::string mVerb;
	std::string mPath;
	std::string mVersion;

	//Fields
	std::string mAccept;
	std::string mAcceptLa;
	std::string mAcceptEncoding;
	std::string mUserAgent;
	std::string mHost;
	std::string mConnection;
};

struct  SendJob
{
	Request p_cRequest;
	TcpSocket* xSock;
	int status;
	//void* p_xData;
	//int size;
	//IPAddr remoteAddr;
};

class Server
{
public:
	Server();
	~Server();

	enum
	{
		MAX_SEND_JOBS = 10,
		MAX_CON = 10,
		PORT = 80,
		MAX_REQ_LINES = 20,
		MAX_REQ_CHARS_PER_LINE = 512,
		CONNECTION_TIMEOUT_SEC = 600
	};

	bool Init();
	void Run();
	void Close();

	void HandleRequest(Request& p_xRequest, void* p_xData, int p_iSize);
	bool CheckRecvEnd(char* data);

	void CheckInput();
	void CheckNewConnections();
	void SendThread();

private:
	std::vector<TcpSocket*> m_xaConnections;
	TcpAcceptSocket m_xAcceptSocket;
	ThreadLock m_xLock;
	bool bQuit;
	int m_iThreadsRunning;
	int m_iNumSendJobs;
};

#endif	// SERVER_H_INCLUDED